package com.along.game.action.manage;

import java.io.IOException;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;

import com.along.framework.utils.FgConstant;
import com.along.framework.utils.FileUpVO;
import com.along.framework.utils.PatinationUtil;
import com.along.framework.web.BaseAction;

import com.along.game.model.Smallpic;
import com.along.game.service.BaseInfoService;
import com.along.game.service.CommonUtilService;

public class SmallPicAction  extends BaseAction{

	/**
	 * 
	 */
	private static final long serialVersionUID = 5948374325784391763L;
	@Autowired
	private BaseInfoService baseInfoService;
	@Autowired
	private CommonUtilService commonUtilService;
	
	private FileUpVO  fileUpVO;
	private Smallpic smallpicVO;

	public Smallpic getSmallpicVO() {
		return smallpicVO;
	}

	public void setSmallpicVO(Smallpic smallpicVO) {
		this.smallpicVO = smallpicVO;
	}
	private Map if_using =new LinkedHashMap(); 
	public Map getIf_using() {
		return FgConstant.IF_USING;
	}


	public void setIf_using(Map if_using) {
		this.if_using = if_using;
	}


	
	/**
	 * 查询大图列表
	 */
	public String querySmallPic()
	{
	       HttpServletRequest request=getRequest();
			String currentpage=request.getParameter("currentpage");
	        int  totalRows =baseInfoService.querySmallPicCount(smallpicVO);
			currentpage=PatinationUtil.getCurrentPage(currentpage);
			int startrow=PatinationUtil.getStartRow(currentpage);
			int endrow=PatinationUtil.getEndRow(currentpage);
			List smallPicList =baseInfoService.querySmallPic(startrow, endrow, smallpicVO);
			currentpage=PatinationUtil.getCurrentPage(currentpage);
            request.setAttribute("totalRows", totalRows);
			request.setAttribute("currentpage", currentpage);
			request.setAttribute("smallPicList", smallPicList);
	
return "/baseinfo/smallpiclist.jsp";
	}
	
	
	/**
	 * 进入小图片修改页
	 */
	public String editSmallPic()
	{
	       HttpServletRequest request=getRequest();
		
			
			
			 if(smallpicVO!=null&&smallpicVO.getId()!=null)
			  {
				 
			
				  request.setAttribute("addorupdate", "update");
				  smallpicVO=baseInfoService.querySmallPicDetail(String.valueOf(smallpicVO.getId()));
				
			  }
			  else{
		
				  request.setAttribute("addorupdate", "add");
			  }
				 
			
				
return "/baseinfo/smallpicedit.jsp";
	}
	
	
	
	/**
	 * 执行修改、增加小图片信息
	 * @return
	 * @throws IOException 
	 * @throws BusException
	 */
	public String postSmallPic() throws IOException{
	
	
	 HttpServletRequest request=getRequest();
 HttpServletResponse response=getResponse();
 String addorupdate=request.getParameter("addorupdate");

	String message="";
	String filePath= request.getRealPath("/images/initpic");
	String fileName ="";
	String preImgFileName="";
	String tipMessage="";

	if(fileUpVO!=null) 
	{
		fileUpVO.setFilePath(filePath+"/");

	//fileUpVO.setUploadFileName(bigpicVO.getPicUrl());
	preImgFileName =commonUtilService.fileUpLoad(fileUpVO);

	}


	if(fileUpVO==null||fileUpVO.getUploadFileName().trim().equals(""))
	{
		tipMessage+=",未选择图片上传;";
	}
		else
		{
			smallpicVO.setPicUrl(preImgFileName);
			tipMessage+=",图片已上传;";
		}
 
 

		if(addorupdate.equals("add"))
		{
			baseInfoService.loadSmallPic(smallpicVO);
				message="小图片添加成功";
				
		}
		else if(addorupdate.equals("update"))
		{
		
			baseInfoService.saveSmallPic(smallpicVO);
				message="小图片修改成功";
		
			
		}
		else  if(addorupdate.equals("remove")){
	
			baseInfoService.removeGameTable(String.valueOf(smallpicVO.getId()));
		
		
		message="小图片删除成功";
		}
		 response.setContentType("text/html;charset=UTF-8");
		response.getWriter().println("<script>alert('"+message+tipMessage+"');this.location.href='smallPic_querySmallPic.do?smallpicVO.bigpicId="+smallpicVO.getBigpicId()+"';</script>");
		response.flushBuffer();
		  return  null;
	
		//JSONUtil.writerJSON(list, response.getWriter(),java.util.Date.class);

	}

	public FileUpVO getFileUpVO() {
		return fileUpVO;
	}

	public void setFileUpVO(FileUpVO fileUpVO) {
		this.fileUpVO = fileUpVO;
	}
	
	
	
	
	
}
